package cn.yonyong.base.project.controller;

import cn.yonyong.base.project.entity.TblBatchappear;
import cn.yonyong.base.project.service.BatchAppearService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Describtion Todo
 * @Author yonyong
 * @Date 2019/9/6 17:09
 * @Version 1.0.0
 **/
@Api(value = "/batch", tags = "批次接口")
@RestController
@RequestMapping("batch")
public class BatchAppearController {

    @Autowired
    private BatchAppearService batchAppearService;

    @ApiOperation(value = "分页获取batch列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "offset", value = "起始页", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "limit", value = "分页条数", required = true, dataType = "int", paramType = "path"),
    })
    @RequestMapping(value="getAll/{offset}/{limit}",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public JSONObject getAll(@PathVariable("offset")int offset, @PathVariable("limit")int limit){
        Map<String,Object> map = new HashMap<>();
        List<TblBatchappear> list = batchAppearService.getAllLists(offset,limit);
        map.put("list",list);
        return (JSONObject) JSONObject.toJSON(map);
    }

    @ApiOperation(value = "根据条件查询批次列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "batch", value = "批次号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "applyName", value = "申请人", required = false, dataType = "String"),
            @ApiImplicitParam(name = "depart", value = "部门", required = false, dataType = "String"),
            @ApiImplicitParam(name = "userid", value = "用户id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "state", value = "批次状态", required = false, dataType = "String"),
            @ApiImplicitParam(name = "minTime", value = "最早创建时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "maxTime", value = "最近创建时间", required = false, dataType = "String"),
    })
    @RequestMapping(value="searchBatch",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public  JSONObject search(@RequestParam(value ="batch",required = false)String batch, @RequestParam(value ="applyName",required = false)String applyName, @RequestParam(value ="depart",required = false)String depart,
                              @RequestParam(value ="userid",required = true)String userid, @RequestParam(value ="state",required = false)String state, @RequestParam(value ="minTime",required = false)String minTime,
                              @RequestParam(value ="maxTime",required = false)String maxTime, HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        Map<String,Object> map = new HashMap<>();
        TblBatchappear tblBatchappear = new TblBatchappear();
        tblBatchappear.setBatch(batch);
        tblBatchappear.setApplyName(applyName);
        tblBatchappear.setCommen2(depart);
        tblBatchappear.setUserid(userid);
        tblBatchappear.setState(state);
        List<TblBatchappear> list = batchAppearService.getListByExample(tblBatchappear,minTime,maxTime);
        map.put("result",list);
        return (JSONObject) JSONObject.toJSON(map);
    }
}
